import React, { Component } from 'react';
import {user_register} from '../utils/api'

class MyRegister extends Component {
    constructor(props){
        super(props)
        this.state={
            phone:"",
            pass:"",
            checkpass:""
        }
    }
    register(){
        if(this.state.phone&&this.state.pass&&this.state.checkpass){
            if(this.state.pass===this.state.checkpass){
                var {checkpass,...params}=this.state;
                user_register(params).then((res)=>{
                    if(res.data.code===200){
                        alert(res.data.msg)
                    }else{
                        alert(res.data.msg)
                    }
                })
            }else{
                alert("两次密码不一致")
            }
        }
        

    }
    handleChange(e){

        this.setState({
            [e.currentTarget.name]:e.currentTarget.value
        })
    }
    render() {
        return (
            <div className='myregister'>
                   <div className='title'>用户注册</div>
                <div className="register">
                    <div className="line">
                    <input type="text"  placeholder='请输入账号' name='phone' defaultValue={this.state.phone} onChange={(e)=>{this.handleChange(e)}}/>
                    </div>
                    <div className="line">
                    <input type="password" placeholder='请输入密码' name='pass'defaultValue={this.state.pass} onChange={(e)=>{this.handleChange(e)}}/>
                    </div>
                    <div className="line">
                    <input type="password" placeholder='请确认密码' name='checkpass' defaultValue={this.state.checkpass} onChange={(e)=>{this.handleChange(e)}}/>
                    </div>
                    <div className="line">
                    <button onClick={()=>{this.register()}}>注册</button>
                    </div>
                </div>
            </div>
        );
    }
}

export default MyRegister;